package com.huixuebao.insure.controller;

import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.bean.ResponseData;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.CommonUtil;
import com.huixuebao.insure.common.config.GlobalConfig;
import com.huixuebao.insure.dto.MerchantDTO;
import com.huixuebao.insure.entity.MerchantBasicsEntity;
import com.huixuebao.insure.service.MerchantBasicsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TODO
 *
 * @author yufatao
 * @version 1.0
 * @date 2020-10-31 22:04
 */
@Slf4j
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    MerchantBasicsService merchantBasicsService;

    @Autowired
    GlobalConfig globalConfig;

    @GetMapping("/list")
    public ApiResult listMerchants() {
        //ResponseData responseData = new ResponseData();
        ApiResult apiResult = new ApiResult();
        try {
            List<MerchantDTO> list = merchantBasicsService.listMerchants();
            //responseData = CommonUtil.getResponseData(list, globalConfig.getPrivateKey(),
                    //globalConfig.getDataKey());
            apiResult.setData(list);
        } catch (Exception e) {
            log.error("获取机构列表异常: {}", e);
            //responseData.setCode(ResultCodeEnum.FAILURE.getCode());
            //responseData.setMessage(ResultCodeEnum.FAILURE.getMessage());
        }
        return apiResult;
    }
    @GetMapping("/getMerchantsDetails")
    public ResponseData getMerchantsDetails(@RequestParam("id") String id){
        ResponseData responseData = new ResponseData();
        try {
            List<MerchantBasicsEntity> list = merchantBasicsService.getMerchantsDetails(id);
            log.info("merchant list = {}", list);
            responseData = CommonUtil.getResponseData(list, globalConfig.getPrivateKey(),
                    globalConfig.getDataKey());
        }catch (Exception e){
            log.error("获取机构详情失败{}:");
            responseData.setCode(ResultCodeEnum.FAILURE.getCode());
            responseData.setMessage(ResultCodeEnum.FAILURE.getMessage());
        }
        return responseData;
    }

    @GetMapping("/getListByStatus")
    public List<MerchantBasicsEntity> getListByStatus() {
        return merchantBasicsService.getListByStatus();
    }
 }